package com.codingke.codingbookstore.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.codingke.codingbookstore.R;
import com.codingke.codingbookstore.adapter.BookListItemAdapter;
import com.codingke.codingbookstore.ui.BookDetailActivity;
import com.codingke.codingbookstore.ui.MainActivity;
import com.codingke.codingbookstore.vo.BookInfo;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

/**
 * descreption:
 * company: codingke.com
 * Created by vince on 15/11/16.
 */
public class AndroidCategoryFragment extends BaseFragment implements AdapterView.OnItemClickListener{

    private MainActivity mainActivity;
    private ListView listView2_category_android;
    private LinearLayout load_layout;
    private ArrayList<BookInfo> bookInfos = new ArrayList<>();
    private BookListItemAdapter bookListItemAdapter;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mainActivity = (MainActivity) getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_category,null);
        listView2_category_android = (ListView) view.findViewById(R.id.listView2_category);
        listView2_category_android.setOnItemClickListener(this);
        load_layout = (LinearLayout) view.findViewById(R.id.load_layout);
        bookListItemAdapter = new BookListItemAdapter(mainActivity,bookInfos);
        listView2_category_android.setAdapter(bookListItemAdapter);
        loadData();
        return view;
    }

    //初始化数据
    private void loadData() {
        if(bookInfos.size()==0){
            load_layout.setVisibility(View.VISIBLE);
            listView2_category_android.setVisibility(View.GONE);
            BmobQuery<BookInfo> query = new BmobQuery<>();
            query.setCachePolicy(BmobQuery.CachePolicy.CACHE_ELSE_NETWORK);
            query.addWhereEqualTo("categoryId", "FcUtAAAg");
            query.findObjects(mainActivity, new FindListener<BookInfo>() {
                @Override
                public void onSuccess(List<BookInfo> list) {
                    load_layout.setVisibility(View.GONE);
                    listView2_category_android.setVisibility(View.VISIBLE);
                    bookInfos = (ArrayList<BookInfo>) list;
                    bookListItemAdapter.setBookInfos(bookInfos);
                    bookListItemAdapter.notifyDataSetChanged();
                }

                @Override
                public void onError(int i, String s) {
                    load_layout.setVisibility(View.GONE);
                    listView2_category_android.setVisibility(View.VISIBLE);
                }
            });
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        BookInfo bookInfo = bookInfos.get(position);
        Intent intent = new Intent(mainActivity, BookDetailActivity.class);
        intent.putExtra("bookInfo",bookInfo);
        startActivity(intent);

    }
}
